package cai.test;

import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Queue;
import java.util.Set;

import org.junit.Test;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

public class CaiExecute {


	
	public static void t1(int n, String filePath, String time) throws Exception {
		
		int num = 1;
        int maxnum = 0; //最高期数保留
        String maxNumInfo = null;
        int maxnumCishu = 0;
        Integer ben = null, l_ben = 8;
        
        Set<Integer> x = new HashSet<>(Arrays.asList(1,3,5,6,8,10));
		//Set<Integer> y = new HashSet<>(Arrays.asList(2,3,4,7,8,9));
		
        List<Integer> y1 = new ArrayList<>();
        
    	StringBuilder sb = new StringBuilder();
    	String nextOpenTime = null;
		JSONArray array = data(filePath);
		Queue<Integer> queue = new LinkedList<>();
		for (int size = array.size(); size > 0; size--) {
			sb.delete(0, sb.length());
            JSONObject o = array.getJSONObject(size-1);
            String lotteryOpen = o.getString("lotteryOpen");
            sb.append(o.getString("gameName")).append("\t\t").append(o.getString("openTime")).append("\t").append(lotteryOpen).append("\t").append(filePath);
            sb.append("\t\t数字：").append(n);
           
            if(ben != null) {
            	sb.append("\t\t本期：").append(ben).append("\t\tnum: ");
            	sb.append(num).append("\t\t").append(x);
        		if(x.contains(ben)) {
        			num = 1;
        		}else {
        			num++;
        			if(num == 9) { //超N期不中
            			maxnumCishu ++;
                    	System.out.println("\n"+sb);
            		}
        		}
            	
//            	if(num%2 == 0) {	
//            	}else {
//            	}
            	
        		
            }
            
            if(num > maxnum) {
                if(time != null) {
            		if(o.getString("openTime").startsWith(time)) {
            			maxnum = num; //保留最高
		                maxNumInfo = sb.toString();
            		};
            	}else {
            		maxnum = num; //保留最高
	                maxNumInfo = sb.toString();
            	}
            }
            
            //System.out.println(sb);
            
            String[] split = lotteryOpen.split(",");
            for (int i = 0; i < split.length; i++) {
                if(split[i].equals(n+"") && size-2 >= 0) {
                    Integer next = Integer.parseInt(array.getJSONObject(size-2).getString("lotteryOpen").split(",")[i]);
                    ben = next;
                    break;
                };
            }
		}
		System.out.println("最高：" + maxNumInfo + ",\t 大于8次的出现次数：" + maxnumCishu);
	}


	public static void t3(int index, String filePath, String time) throws Exception {
		int num = 1;
		int maxnum = 0; //最高期数保留
		String maxNumInfo = null;
		int maxnumCishu = 0;

		Set<Integer> x = new HashSet<>(Arrays.asList(1,2,3,4,5));
		StringBuilder sb = new StringBuilder();
		JSONArray array = data(filePath);
		String filePathJie = filePath.substring(filePath.indexOf("/cai_file"));
		for (int i = 0; i < array.size(); i++) {
			sb.delete(0, sb.length());
			JSONObject o = array.getJSONObject(i);
			String lotteryOpen = o.getString("lotteryOpen");
			sb.append(o.getString("gameName")).append("\t\t").append(o.getString("openTime")).append("\t").append(lotteryOpen).append("\t").append(filePathJie);
			sb.append("\t\t数字：").append(index);

			String[] split = lotteryOpen.split(",");
			Integer ben = Integer.parseInt(split[index-1]);


			if(ben != null) {
				sb.append("\t\t本期：").append(ben).append("\t\tnum: ");
				sb.append(num).append("\t\t").append(x);
				if(x.contains(ben)) {
					if(num == 3) {
						sb.append("\t\t第" + num + "次，中");
						System.out.println(sb);
					}
					num = 1;
				}else {
					num++;
					if(num >= 7) { //超N期不中
						maxnumCishu ++;
						//System.out.println("\n"+sb);
					}
				}
			}
		}
	}

	public static void t4(int index, String filePath, String time) throws Exception {
		int num = 1;
		int maxnum = 0; //最高期数保留
		String maxNumInfo = null;
		int maxnumCishu = 0;

		Set<Integer> x = new HashSet<>(Arrays.asList(1,2,3,4,5));
		StringBuilder sb = new StringBuilder();
		JSONArray array = data(filePath);
		for (int size = array.size(); size > 0; size--) {
			sb.delete(0, sb.length());
			JSONObject o = array.getJSONObject(size-1);
			String lotteryOpen = o.getString("lotteryOpen");
			sb.append(o.getString("gameName")).append("\t\t").append(o.getString("openTime")).append("\t").append(lotteryOpen);//.append("\t").append(filePath);
			sb.append("\t\t数字：").append(index);

			String[] split = lotteryOpen.split(",");
			Integer ben = Integer.parseInt(split[index-1]);


			if(ben != null) {
				sb.append("\t\t本期：").append(ben).append("\t\tnum: ").append(num);
				//sb.append("\t\t").append(x);
				if(x.contains(ben)) {
					if(num == 3 && o.getString("openTime").startsWith(time)) {
						sb.append("\t\t第" + num + "次，中");
						System.out.println(sb);
					}
					num = 1;
				}else {
					num++;
//					if(num >= 6 && o.getString("openTime").startsWith(time2)) { //超N期不中
//						maxnumCishu ++;
//						//System.out.println("\n"+sb);
//					}
				}
			}
		}
	}

	public static void t2(int index, String filePath, String time) throws Exception {
		int num = 0;
        int maxnum = 0; //最高期数保留
        String maxNumInfo = null;
        int maxnumCishu = 0;
        
        Set<Integer> x = new HashSet<>(Arrays.asList(1,2,3,4,5));
        Set<Integer> y = new HashSet<>(Arrays.asList(6,7,8,9,10,3));
        
        StringBuilder sb = new StringBuilder();
		JSONArray array = data(filePath);
		//Queue<Integer> queue = new LinkedList<>();
		//for (int size = array.size(); size > 0; size--) {

		String filePathJie = filePath.substring(filePath.indexOf("/cai_file"));
		Integer l_ben = 8;
		for (int i = 0; i < array.size(); i++) {
			sb.delete(0, sb.length());
			 JSONObject o = array.getJSONObject(i);
			 String lotteryOpen = o.getString("lotteryOpen");
	         sb.append(o.getString("gameName")).append("\t\t").append(o.getString("openTime")).append("\t").append(lotteryOpen).append("\t").append(filePathJie);
	         sb.append("\t\t数字：").append(index);

	         String[] split = lotteryOpen.split(",");
	         Integer ben = Integer.parseInt(split[index-1]);

//			 if(o.getString("openTime").equals("06:00:05")) {
//				 System.out.println("ben = " + ben);
//			 }

			//if(x.contains(ben.intValue()) || l_ben == ben.intValue()) {
			if(x.contains(ben.intValue())) {
				num = 0; l_ben = 8;
				sb.append("\t 本期：").append(ben).append("\t\t num: ").append(num);
			}else {
				num ++;
				l_ben = ben;
				sb.append("\t 本期：").append(ben).append("\t\t num: ").append(num);
			}

	         if(num > maxnum) {
                if(time != null) {
            		if(o.getString("openTime").startsWith(time)) {
            			maxnum = num; //保留最高
		                maxNumInfo = sb.toString();
            		};
            	}else {
            		maxnum = num; //保留最高
	                maxNumInfo = sb.toString();
            	}
	         }

			if(num == 6) { //超N期不中
				maxnumCishu ++;
				System.out.println("-----------超期\t"+sb);
			}else {
				//System.out.println(sb);
			}

		}
		System.out.println("最高：" + maxNumInfo + ",\t 大于n次的出现次数：" + maxnumCishu);
	}

	public static void kanSaiCheXiaDafa(String filePath_s, String filePath_d) throws Exception {
		JSONArray datas = data(filePath_s);
		Map<String, String> sMap = new HashMap<>();
		for (int i = datas.size() - 1; i >= 0; i--) {
			JSONObject data = datas.getJSONObject(i);
			String openTime = data.getString("openTime").substring(0, 5);
			String[] lotteryOpenArr = data.getString("lotteryOpen").split(",");
			int index = 0;
			for (int j = 0; j < lotteryOpenArr.length; j++) {
				if(lotteryOpenArr[j].equals("8")) {
					index = j;
					break;
				};
			}
			String s = JSON.toJSONString(lotteryOpenArr);
			sMap.put(openTime, index+"");
			System.out.println("openTime: " + openTime + ",index: " + index + ",lotteryOpenArr: " + s);
		}

		//JSONArray d_datas = data(filePath_d);

	}
	public static final StringBuilder sb = new StringBuilder();
	public static JSONArray data(String filePath) throws Exception {
		sb.delete(0, sb.length());
		List<String> l = Files.readAllLines(Paths.get(filePath));
		for (String s : l) sb.append(s);
		String jsonStr = sb.toString().replaceAll("\\s*|\t|\r|\n", "");
        return JSON.parseObject(jsonStr).getJSONObject("data").getJSONObject("backData").getJSONArray("lotteryOpen");
	}


	public static void hebing(List<String> filePaths, String he) throws Exception {
		File file = new File(he);
		if(file.exists()) file.delete();
		file.createNewFile();

//		FileInputStream fileInputStream = new FileInputStream(he);
		StringBuilder sb = new StringBuilder();
//		byte[] buf = new byte[1024*10];
//		while(fileInputStream.read(buf) != -1) sb.append(new String(buf,"utf-8"));
//		fileInputStream.close();

		String jsonStr = sb.length() != 0? sb.toString().replaceAll("\\s*|\t|\r|\n", ""): "{'data': {'backData':{'lotteryOpen':[]}}}";
		JSONObject object = JSON.parseObject(jsonStr);
		JSONArray hejson = object.getJSONObject("data").getJSONObject("backData").getJSONArray("lotteryOpen");;

		Set<String> isjSON = new HashSet<>();
		for (String filePath : filePaths) {
			JSONArray lotteryOpen = data(filePath);
			for (int i = lotteryOpen.size() - 1; i >= 0; i--) {
				JSONObject o = (JSONObject)lotteryOpen.get(i);
				String issueNo = o.getString("issueNo");
				if(!isjSON.contains(issueNo)) {
					isjSON.add(issueNo);
					//System.out.println("o = " + o);
					hejson.add(o);
				}
			}
		}

//		for (Object o : hejson) {
//			System.out.println("o1 = " + o);
//		}

		FileOutputStream fos = new FileOutputStream(he);
		OutputStream oos = new BufferedOutputStream(fos);

		OutputStreamWriter outputStreamWriter = new OutputStreamWriter(oos, "UTF-8");

		//String str = "我唉呗甲方十点多\n  第三方问问";
		//byte[] buf = str.getBytes("UTF-8");
		//oos.write(buf);
		//oos.close();

		outputStreamWriter.write(object.toString());
		outputStreamWriter.close();
	}
	
}
